package com.harunasan.carrental.api.serve.controller;


import com.harunasan.carrental.api.serve.domain.entity.Store;
import com.harunasan.carrental.api.serve.domain.post.StorePost;
import com.harunasan.carrental.api.serve.service.IStoreService;
import com.harunasan.carrental.common.api.CommonPage;
import com.harunasan.carrental.common.api.CommonResult;
import com.harunasan.carrental.common.query.CommonQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 梁毅
 * @since 2022-03-07 23:06:16
 */
@RestController
@Api(tags = "门店管理")
@RequestMapping("/api/serve/store")
public class StoreController {
    @Autowired
    private IStoreService storeService;

    @PostMapping()
    @ApiOperation("创建网点")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> storeCreate(@RequestBody StorePost storePost) {
        return CommonResult.success(storeService.create(storePost));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除网点信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> storeRemove(@ApiParam(name = "id", value = "网点标识", required = true)
                                             @PathVariable Long id) {
        return CommonResult.success(storeService.delete(id));
    }

    @PutMapping("/{id}")
    @ApiOperation("修改网点信息")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> storeUpdate(@ApiParam(name = "id", value = "网点标识", required = true)
                                             @PathVariable Long id, @RequestBody StorePost storePost) {
        return CommonResult.success(storeService.update(id,storePost));
    }

    @GetMapping()
    @ApiOperation("获取网点列表")
    public CommonResult<List<Store>> storeList(CommonQueryParam commonQueryParam) {
        commonQueryParam.setSize((int) storeService.count());
        return CommonResult.success(storeService.list(commonQueryParam));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取网点详情")
    public CommonResult<Store> storeObj(@ApiParam(name = "id", value = "网点标识", required = true)
                                        @PathVariable Long id) {
        return CommonResult.success(storeService.detail(id));
    }

    @GetMapping("/page")
    @ApiOperation("获取网点分页")
    public CommonResult<CommonPage<Store>> storePage(CommonQueryParam commonQueryParam) {
        return CommonResult.success(CommonPage.restPage(storeService.pageList(commonQueryParam)));
    }
}
